<?php
/**
 * @Author: rocky
 * Date: 2019/9/28
 * Time: 11:22
 */


namespace app\shop\controller\api\v1;


use app\common\controller\BaseApiController;
use app\common\model\ShopAgent;
use app\common\model\ShopStore;
use app\common\facade\Token;
use app\common\model\User;
use app\shop\model\ShopOrder;
use app\shop\resource\AgentStoreResoure;
use think\Validate;

/**
 * 代理商管理
 * Class Agent
 * @package app\shop\controller\api\v1
 */
class Agent extends BaseApiController
{
    public function getInfo()
    {
        $id = Token::getVar('id');
        $res = ShopAgent::field('id,name,username,phone,type,region,logo')->where('id',$id)->find();
        if ($res){
            switch ($res['type']){
                case 1:
                    $res['region_type'] = '省代';
                    break;
                case 2:
                    $res['region_type'] = '市代';
                    break;
                default:
                    $res['region_type'] = '区代';
                    break;
            }
            $this->successCode($res);
        }else{
            $this->errorCode(100,'获取失败');
        }
    }

    public function getDetail()
    {
        $id = $this->request->get('id');
        $res = ShopAgent::where('id',$id)->find();
        if ($res){
            switch ($res['type']){
                case 1:
                    $res['region_type'] = '省代';
                    break;
                case 2:
                    $res['region_type'] = '市代';
                    break;
                default:
                    $res['region_type'] = '区代';
                    break;
            }
            $this->successCode($res);
        }else{
            $this->errorCode(100,'获取失败');
        }

    }

    public function postInfo()
    {
        $data = $this->request->only(['logo', 'name']);
        $data['id']= Token::getVar('id');
        $res = ShopAgent::update($data);
        if ($res){
            $this->successCode();
        }else{
            $this->errorCode(100,'修改失败');
        }

    }

    /*
   * 店铺列表
   */
    public function getStore()
    {
        $region = Token::getVar('region');
        $region_type = Token::getVar('type');
        switch ($region_type){
            case 1:
                $where['province'] = $region;
                break;
            case 2:
                $where['city'] = $region;
                break;
            default:
                $where['area'] = $region;
        }
        $time = $this->request->get('time');
        $data = ShopStore::where($where)->where('status',1)->pages()->select();
        $this->successCode(AgentStoreResoure::collection($data));
    }

    /*
   * 下级代理商列表
   */
    public function getList()
    {
        $region = Token::getVar('region');
        $region_type = Token::getVar('type');
        switch ($region_type){
            case 1:
                $where['province'] = $region;
                $where['type'] = ['neq',1];
                break;
            case 2:
                $where['city'] = $region;
                $where['type'] = ['=',3];
                break;
            default:
                $where['area'] = $region;
        }
        $keyword = $this->request->get('keyword');
        if ($keyword){
            $data['keyword'] = $keyword;
            $validate = Validate::make()->rule('keyword', '/^1\d{10}$/')->check($data);
            if ($validate) {
                $where['phone'] = $keyword;
            }else{
                $where['name'] = $keyword;
            }
        }
        $where['id'] = ['neq',Token::getVar('id')];
        $data = ShopAgent::field('id,name,region,phone,address,desc')->where($where)->where('status',1)->select();
        $this->successCode($data);
    }

    /*
     * 总营业额统计
     */
    public function getBusiness()
    {
        $store_id = $this->request->get('store_id');
        if (!$store_id) return $this->errorCode(100,'Invalid parameter');
        $time = $this->request->get('time');
        $query = ShopOrder::where('store_id',$store_id)->where('status','neq','-1')->where('is_pay',1);
        $data = [];
        if ($time == 'quarter'){
            $season = ceil((date('n'))/3);//当月是第几季度
            $begain_date = date('Y-m-d', mktime(0, 0, 0,$season*3-3+1,1,date('Y')));
            $now_date = date('Y-m-d',strtotime('+1 day'));
            $data['list'] = $query->whereBetweenTime('create_at', $begain_date,$now_date)->pages()->select();
        }else{
            $data['list'] = $query->whereTime('create_at',$time)->pages()->select();
        }
        $data['total_price'] =  ShopOrder::where('store_id',$store_id)->where('status','neq','-1')->where('is_pay',1)->sum('pay_price');
        $this->successCode($data);
    }


    /*
    * 会员统计
    */
    public function getMember()
    {
        $store_id = $this->request->get('store_id');
        if (!$store_id) return $this->errorCode(100,'Invalid parameter');
        $time = $this->request->get('time');
        $query = User::field('id,headimg,nickname,create_at')->where('store_id',$store_id);
        if ($time == 'quarter'){
            $season = ceil((date('n'))/3);//当月是第几季度
            $begain_date = date('Y-m-d', mktime(0, 0, 0,$season*3-3+1,1,date('Y')));
            $now_date = date('Y-m-d',strtotime('+1 day'));
            $data['list'] = $query->whereBetweenTime('create_at', $begain_date,$now_date)->pages()->select();
            $data['total_member'] = User::where('store_id',$store_id)->whereBetweenTime('create_at', $begain_date,$now_date)->count();
        }else{
            $data['list'] = $query->whereTime('create_at',$time)->pages()->select();
            $data['total_member'] = User::where('store_id',$store_id)->whereTime('create_at',$time)->count();
        }
        $this->successCode($data);
    }


    /*
     * 预约消费
     */
    public function getSubscribe()
    {
        $store_id = $this->request->get('store_id');
        if (!$store_id) return $this->errorCode(100,'Invalid parameter');
        $begain_date = $this->request->get('begain_date');
        $end_date = $this->request->get('end_date');
        $rule = [
            'begain_date'  => 'requireWith:end_date',
            'end_date'  => 'requireWith:begain_date'
        ];
        $msg = [
            'begain_date.requireWith' => '请选择开始时间',
            'end_date.requireWith'     => '请选择结束时间'
        ];
        $validate = Validate::make($rule,$msg);
        $data = [
            'begain_date' => $begain_date,
            'end_date' => $end_date,
        ];
        if (!$validate->check($data)) {
            $this->errorCode(100,$validate->getError());
        }
        $query = ShopOrder::where('store_id',$store_id)->where('type', 1)->where('status','neq','-1')->where('is_pay',1);
        if ($begain_date && $end_date){
            $now_date = date('Y-m-d',strtotime($end_date) + 86400);
            $res['today_num'] = $query->whereTime('create_at', 'today')->sum('buy_num');
            $res['total_num'] = $query->whereBetweenTime('create_at', $begain_date,$now_date)->sum('buy_num');
        }else{
            $res['today_num'] = $query->whereTime('create_at', 'today')->sum('buy_num');
            $res['total_num'] = $query->sum('buy_num');
        }
        $this->successCode($res);
    }

   /*
   * 会员充值
   */
    public function getRecharge()
    {
        $store_id = $this->request->get('store_id');
        if (!$store_id) return $this->errorCode(100,'Invalid parameter');
        $time = $this->request->get('time');
        $data['list'] = ShopOrder::where('store_id',$store_id)->where('type', 5)->where('status','neq','-1')->where('is_pay',1)->whereTime('create_at',$time)->pages()->select();
        $this->successCode($data);
    }

    /*
    * 会员卡购买
    */
    public function getMemberCard()
    {
        $store_id = $this->request->get('store_id');
        if (!$store_id) return $this->errorCode(100,'Invalid parameter');
        $time = $this->request->get('time');
        $query = ShopOrder::where('store_id',$store_id)->where('type', 6)->where('status','neq','-1')->where('is_pay',1);
        if ($time == 'quarter'){
            $season = ceil((date('n'))/3);//当月是第几季度
            $begain_date = date('Y-m-d', mktime(0, 0, 0,$season*3-3+1,1,date('Y')));
            $now_date = date('Y-m-d',strtotime('+1 day'));
            $data['list'] = $query->whereBetweenTime('create_at', $begain_date,$now_date)->pages()->select();
        }else{
            $data['list'] = $query->whereTime('create_at',$time)->pages()->select();
        }
        $this->successCode($data);
    }
}